package com.asuka.controller.user;

import com.asuka.dto.OrderPageDTO;
import com.asuka.dto.OrderPayDTO;
import com.asuka.dto.OrdersSubmitDTO;
import com.asuka.entity.Orders;
import com.asuka.result.PageResult;
import com.asuka.result.Result;
import com.asuka.service.OrderService;
import com.asuka.vo.OrderSubmitVO;
import com.asuka.vo.OrderVO;
import com.asuka.vo.WebSocketRemindVO;
import com.asuka.websocket.WebSocketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/user/order")
@RestController("userOrderController")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;


    @Autowired
    private WebSocketService webSocketService;




    //催单
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable Long id){

        orderService.reminder(id);

        return Result.success();
    }

    //再来一单
    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable Long id){
        orderService.repetition(id);
        return Result.success();
    }


    //查询历史订单
    @GetMapping("/historyOrders")
    public Result<PageResult> selectList(OrderPageDTO page){

        PageResult list = orderService.selectPageList(page);
        return Result.success(list);
    }


    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id){

        orderService.cancel(id);
        return Result.success();
    }


    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> orderDetail(@PathVariable Long id){

        OrderVO orderVO = orderService.details(id);

        return Result.success(orderVO);
    }


    @PostMapping("/submit")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO orders){

        OrderSubmitVO submitVO = orderService.OrderController(orders);



        return Result.success(submitVO);
    }




    @PutMapping("/payment")
    public Result<Orders> payOrder(@RequestBody OrderPayDTO orderPayDTO){
        Orders order = orderService.pay(orderPayDTO);

        WebSocketRemindVO webSocketRemindVO = WebSocketRemindVO.builder()
                .type(WebSocketRemindVO.SUBMIT)
                .orderId(order.getId())
                .content("待接单")
                .build();

        webSocketService.sendToAllClient(webSocketRemindVO);

        return Result.success(order);
    }

}
